import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/generated/json/base/json_convert_content.dart';
import 'package:yurun_smartmerchants/models/staff_list_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/routes/routes.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/evenbus_utils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';
import 'package:yurun_smartmerchants/models/delete_staff_entity.dart';

class StaffDetailPage extends StatefulWidget {

  final StaffListContent staff;

  StaffDetailPage({this.staff});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _StaffDetailPageState();
  }
}

class _StaffDetailPageState extends State<StaffDetailPage> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colours.white,
        centerTitle: true,
        iconTheme: IconThemeData(color: Colours.app_words),
        title: Text(
          '员工详情',
          style: TextStyle(fontSize: 18, color: Colours.app_words),
        ),
      ),
      body: Container(
        color: Colours.app_background,
        child: Column(
          children: <Widget>[
            Container(
              color: Colours.white,
              margin: EdgeInsets.only(top: AppSize.height(20)),
              height: AppSize.height(98),
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: Container(
                        margin: EdgeInsets.only(left: AppSize.width(32)),
                        child: Text(
                          '员工姓名',
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: Container(
                        alignment: Alignment.centerRight,
                        margin: EdgeInsets.only(right: AppSize.width(32)),
                        child: Text(
                          widget.staff == null ? '' : widget.staff.name,
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      )),
                ],
              ),
            ),
            Divider(
              thickness: AppSize.height(1),
              height: AppSize.height(1),
              color: Colours.app_background,
            ),
            Container(
              color: Colours.white,
              height: AppSize.height(98),
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: Container(
                        margin: EdgeInsets.only(left: AppSize.width(32)),
                        child: Text(
                          '联系电话',
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: Container(
                        alignment: Alignment.centerRight,
                        margin: EdgeInsets.only(right: AppSize.width(32)),
                        child: Text(
                          widget.staff == null ? '' : widget.staff.phone,
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      )),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: AppSize.height(80),left: AppSize.width(32),right: AppSize.width(32)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Container(
                      margin: EdgeInsets.only(right: AppSize.width(12)),
                      height: AppSize.height(88),
                      child: RaisedButton(
                        onPressed: () {
                          _deleteStaff();
                        },
                        color: Colours.white,
                        child: Text(
                          '删除员工',
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                      margin: EdgeInsets.only(left: AppSize.width(12)),
                      height: AppSize.height(88),
                      child: RaisedButton(
                        onPressed: () {
                          Navigator.pushNamed(context, modify_staff_page,arguments: widget.staff);
                        },
                        color: Colours.app_main,
                        child: Text(
                          '修改员工',
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.white),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  void _deleteStaff(){
    int id = widget.staff.id;
    LoadingUtil.showLoading(context);
    ApiInterface.deleteStaff(id).then((value){
      DeleteStaffEntity deleteStaffEntity = JsonConvert.fromJsonAsT<DeleteStaffEntity>(value);
      bool success = deleteStaffEntity.success;
      if(success){
        showToast('删除成功');
        EventBus().send('refresh_staff','');
      }
    }).whenComplete((){
      LoadingUtil.hideLoading(context);
      Navigator.pop(context);
    });
  }
}
